<?php
	include_once ('db.conf');

		$showkey=true;//for show or not the activation code
	
		/* Connect to a MySQL server */
		$link = mysqli_connect(DBHOST,DBUSER,DBPASS,DBDBNAME);
				
		$userlogin=$_POST["cellnum"];
		$firstname=$_POST["firstname"];
		$lastname=$_POST["lastname"];
		$email=$_POST["email"];
		$yearofbirth=$_POST["yearofbirth"];
		$gender=$_POST["gender"];
		$bankaccount=$_POST["bankaccount"];
		$regpassword=$_POST["regpassword"];
		
		$query="call getCustomer('".$userlogin."');";
		
		$num_rows=0;
		if($result = mysqli_query($link,$query))
			while( $row = mysqli_fetch_assoc($result) )
     			$num_rows ++;   
		
		mysqli_close($link);
    				
		if ($num_rows == 0){//there is not customer with this cell number, so we will save everything to the database
							//we will put there new line to customer table and to blocked customers table
				
		
		
		function str_makerand ($minlength, $maxlength, $useupper, $usespecial, $usenumbers)
{
$charset = "abcdefghijklmnopqrstuvwxyz";
if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ($usenumbers) $charset .= "0123456789";
if ($usespecial) $charset .= "~@#$%^*()_+-={}|]["; // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./";
if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
else $length = mt_rand ($minlength, $maxlength);
for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
return $key;
}
		$newkey=str_makerand (10,10,true,false,true); // this key is for activate account, this key is saved in the blocked customers table and will be send to the cell phone by sms module
		
		if($yearofbirth=='')$yearofbirth=0;
		$link = mysqli_connect(DBHOST,DBUSER,DBPASS,DBDBNAME);		
		$query="call createNewCustomerAccount('".$userlogin."','".$firstname."','".$lastname."',0,'".$bankaccount."','".$email."',".$yearofbirth.",'".$gender."','".$regpassword."');";
		
		if($result = mysqli_query($link,$query ))$picovina=555;
		
			
		//if(mysqli_free_result($result))$picovina=555;
		if(mysqli_close($link))$picovina=555; 
		
		$link = mysqli_connect(DBHOST,DBUSER,DBPASS,DBDBNAME);
		$query="call blockCustomer('".$userlogin."',1,'".$newkey."');";
		if($result = mysqli_query($link,$query ))$picovina=555;	
		//if(mysqli_free_result($result))$picovina=555;
		if(mysqli_close($link))$picovina=555; 
		
	
		echo'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to customers interface</title>

<script type="text/javascript" language="JavaScript"><!--
function kontrola()
{
	var activlogin = self.document.forms.form1.activlogin.value;
	var activcode = self.document.forms.form1.activcode.value;

    var is_ok = false;
    if ((activlogin != "")&&(activcode != "")) is_ok = true;
	
	
	if (is_ok == false) alert("Sorry something is wrong or missing!");
    return is_ok; }
// -->
</script>


<style type="text/css">
<!--
.style5 {color: #0066FF; font-size: 24px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; }
.style7 {
	font-size: 24px;
	font-family: Arial, Helvetica, sans-serif;
	color: #0066FF;
}
.style8 {
	font-size: 24px;
	font-family: Arial, Helvetica, sans-serif;
	color: #FFFFFF;
}
.style12 {
	font-size: 18;
}
.style17 {font-size: 16px}
.style18 {font-family: Arial, Helvetica, sans-serif}
.style20 {color: #FFFFFF}
.style21 {color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; }
-->
</style>
</head>

<body>
<p><img src="pics/main_logo_with_text_1010x12.gif" width="1010" height="124" /></p>
<table width="100%" border="0">
  <tr>
    <td colspan="2" align="left" bgcolor="#FF9900"><blockquote>
      <p><span class="style5"> Succes :-)</span></p>
    </blockquote></td>
  </tr>
  <tr>
    <td width="76%" rowspan="3" align="center" valign="top"><blockquote>
      <p align="justify" class="style7">You were registered successfully,</p>
      <p align="justify" class="style7">in few second will sms with activation code come to your phone. Whit that you can activate your account. You can do it here now  or later on the main page. But remember till activation you can not earn money :-(, so dont waste time.</p>
      </blockquote></td>
    <td width="24%" valign="top" bgcolor="#FF9900"><div align="center" class="style8">
      <p class="style12">account activation
        <label></label>
      </p>
      <form onSubmit="return kontrola();" action="unblockaccount.php" method="post" name="form1" class="style12" id="form1">
        <p>                                      
          <span class="style17">
          <label>cell</label> <br />
          <input name="activlogin" type="text" id="activlogin" value="';
		  echo $userlogin;
		  echo'
		  " />
          <br />
          <label>code</label><br />
          <input name="activcode" type="text" id="activcode" value="';
		  if ($showkey)echo $newkey;
		  echo'" />
          </span>        </p>
        <p>
          <label>
            <input type="submit" name="send" id="send" value="Submit" />
            </label>
        </p>
      </form>
      </div>      </td>
  
  
</table>
</body>
</html>
		';
		
		
		
		
		
		}else{//there is one customer with this cell number, so we will show the same form as before with alert
		echo'
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New customer registration</title>

<script type="text/javascript" language="JavaScript"><!--
function kontrola()
{
	var cell = self.document.forms.form1.cellnum.value;
	var email = self.document.forms.form1.email.value;
	var bankaccount = self.document.forms.form1.bankaccount.value;
	var regpassword = self.document.forms.form1.regpassword.value;
	var regretypepassword = self.document.forms.form1.regretypepassword.value;

    var is_ok = false;
    if ((cell != "")&&(email != "")&&(bankaccount != "")&&(regpassword != "")&&(regpassword ==regretypepassword )) is_ok = true;
	
	
	
    if(is_ok) //the email structure validation
	    {
	       	var at="@"
		var dot="."
		var lat=email.indexOf(at)
		var lstr=email.length
		var ldot=email.indexOf(dot)

		if (email.indexOf(at)==-1)	   is_ok = false;
		if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr) is_ok = false;
		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr) is_ok = false;
		if (email.indexOf(at,(lat+1))!=-1)   is_ok = false;
		if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot)   is_ok = false;
		if (email.indexOf(dot,(lat+2))==-1) is_ok = false;
		if (email.indexOf(" ")!=-1)  is_ok = false;
	    }
       
    
    
    
    
    if (is_ok == false) alert("Sorry something is wrong or missing!");
    return is_ok; }
// -->
</script>


<style type="text/css">
<!--
.style5 {color: #0066FF; font-size: 24px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; }
.style7 {
	font-size: 24px;
	font-family: Arial, Helvetica, sans-serif;
	color: #0066FF;
}
.style8 {
	font-size: 24px;
	font-family: Arial, Helvetica, sans-serif;
	color: #FFFFFF;
}
.style12 {
	font-size: 18;
}
.style17 {font-size: 16px}
.style18 {font-family: Arial, Helvetica, sans-serif}
.style20 {color: #FFFFFF}
-->
</style>
</head>

<body>
<p><img src="pics/main_logo_with_text_1010x12.gif" width="1010" height="124" /></p>
<table width="100%" border="0">
  <tr>
    <td colspan="2" align="left" bgcolor="#FF9900"><blockquote>
      <p><span class="style5"> The cell number is wrong :-(</span></p>
    </blockquote></td>
  </tr>
  <tr>
    <td width="25%" rowspan="3" align="center" valign="top"><div align="left">
      <blockquote>&nbsp; </blockquote>
    </div>      <blockquote>
      <p align="left" class="style7">We are sorry, but cell phone with this number is already registered in our system. Check it again please.</p>
    </blockquote></td>
    <td width="75%" valign="top" bgcolor="#FF9900"><div align="center" class="style8">
      <p class="style12">
        <label></label><span class="style17">
        <label></label> 
          </span></p>
      <form  name="form1" onSubmit="return kontrola();" action="createaccount.php" method="post"  class="style12">
        <table width="322" border="0">
          <tr>
            <td width="7"><div align="right"><span class="style17">*</span></div></td>
            <td width="149"><div align="right"><span class="style17">cell number</span></div></td>
            <td width="152"><label>
              <input name="cellnum" type="text" id="cellnum" value="';
			  echo $userlogin;

			  echo'" maxlength="20" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"></div></td>
            <td><div align="right"><span class="style17">first name</span></div></td>
            <td><label>
              <input name="firstname" type="text" id="firstname" value="';
			echo $firstname;
			  echo'" maxlength="30" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"></div></td>
            <td><div align="right"><span class="style17">last name</span></div></td>
            <td><label>
              <input name="lastname" type="text" id="lastname" value="';
			echo $lastname;
			  echo'" maxlength="30" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style17">*</span></div></td>
            <td><div align="right"><span class="style17">email</span></div></td>
            <td><label>
              <input name="email" type="text" id="email" value="';
			echo $email;
			  echo'" maxlength="50" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"></div></td>
            <td><div align="right"><span class="style17">year of birth</span></div></td>
            <td><label>
              <input name="yearofbirth" type="text" id="yearofbirth" value="';
			echo $yearofbirth;
			  echo'" maxlength="4" />
              </label></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><div align="right"><span class="style17">gender</span></div></td>
            <td align="center" valign="middle"><p class="style17">
              <label>
               	  <input type="radio" name="gender" value="f" id="gender_0"   ';
				
				
				
				if ($gender =='f') echo 'checked="checked"'; 
				 
				 echo' />
                girl</label>
              <label>
                <input name="gender" type="radio" id="gender_1" value="m"';
				
				
				
				if ($gender =='m') echo 'checked="checked"'; 
				 
				 echo'/>
                
				
				
				boy</label>
              <br />
            </p>              
              <label></label></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style17">*</span></div></td>
            <td><div align="right"><span class="style17">bank account number</span></div></td>
            <td><label>
              <input name="bankaccount" type="text" id="bankaccount" value="';
			echo $bankaccount;
			  echo'" maxlength="30" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style17">*</span></div></td>
            <td><div align="right"><span class="style17">password</span></div></td>
            <td><label>
              <input name="regpassword" type="password" id="regpassword" maxlength="30" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"><span class="style17">*</span></div></td>
            <td><div align="right"><span class="style17">retype password</span></div></td>
            <td><label>
              <input name="regretypepassword" type="password" id="regretypepassword" maxlength="30" />
            </label></td>
          </tr>
          <tr>
            <td><div align="right"></div></td>
            <td><div align="right"></div></td>
            <td><label>
              <div align="center">
                <input type="submit" name="send2" id="send2" value="submit" />
                <input type="reset" name="reset" id="reset" value="reset" />
              </div>
            </label></td>
          </tr>
        </table>
        <div align="left"></div>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        </form>
      </div>      </td>
  </tr>
  <tr>
    <td bgcolor="#FF9900" class="style12"><div align="center" class="style18">
      <p class="style12"><span class="style20">or contact us at</span> <a href="mailto:psi2@iot.sdu.dk?subject=Forgotten password :-(&body=Here write your story about forgotten password..." class="style20">psi2@iot.sdu.dk</a></p>
    </div></td>
  </tr>
  
  
  
  
  
  
</table>
</body>
</html>

		
		
		
		';
		
		
		}
		
		

		
		
		



?>